<template>
  <div class="app-box">
    <!-- <button @click="startBar">测试开闸</button>
    <button @click="testReadCard">测试读卡</button>
    <button @click="testWriteCard">测试写卡</button> -->

    <Layout ref="runBox"
            title="GateOutAuto 终端"
            temp="Temp2"
            @handleSuccessMqttMessage="handleSuccessMqttMessage"
            :ic="ic"
            :url="url"
            :readID="false"
            :readIC="true"></Layout>

  </div>
</template> 
<script>
import {
  log,
} from '@/utils/message'
import Layout from '../core/layout.vue'
import { GSAConstants } from '@/utils/access'
import { ipcRenderer } from 'electron'

export default {
  components: { Layout },
  data () {
    return {
      ic: GSAConstants().TOPIC_PREADVICE_GATE_OUT_CHECK.ic,
      url: GSAConstants().TOPIC_PREADVICE_GATE_OUT_CHECK.url
    }
  },
  created () {
  },
  mounted () {
    setTimeout(() => {
      this.init()
    }, 3000)
  },
  methods: {
    init () {
      log('开始连接砸口 ====》 ')
      ipcRenderer.send('connect-bar')
      ipcRenderer.on('connect-bar-reply', (event, res) => {
        log('connect-bar-reply', res)
      })
      ipcRenderer.on('raise-bar-reply', (event, res) => {
        log('raise-bar-reply', res)
        if (res.code === 200) {
          this.showMsg('开闸成功！', 'green')
        } else {
          this.showMsg('开闸失败, 请联系服务台！', 'error')
        }
      })

      ipcRenderer.on('write-card-reply', (event, res) => {
        log('write-card-reply', res)
        if (res.readCode === 1) {
          this.showMsg('放行信息写入成功， 欢迎下次再来！', 'green')
        } else {
          this.showMsg(this.parseCardCode(res.readCode), 'error')
        }
      })

      ipcRenderer.on('read-card-reply', (event, res) => {
        log('read-card-reply', res)

        if (res.readCode === 1) {
          log('TestReadCardSuccess', JSON.stringify(res))

          this.$refs.runBox.showMsg(msg, res)
        } else {
          this.showMsg(this.parseCardCode(res.readCode), 'error')
        }
      })
    },
    showMsg (msg, type) {
      this.$refs.runBox.showMsg(msg, type)
    },
    testWriteCard () {

      const qd = {
        p8: '12345678',
        p9: '12345679'
      }
      ipcRenderer.send('write-card', qd)
    },
    sleep (number) {
      var now = new Date();
      var exitTime = now.getTime() + number * 1000;
      while (true) {
        now = new Date();
        if (now.getTime() > exitTime)
          return
      }
    },
    testReadCard () {
      // for (let index = 0; index < 3; index++) {
      //   this.sleep(3)
      //   ipcRenderer.send('read-card', { mode: 4 })
      // }
      ipcRenderer.send('read-card', { mode: 4 })
    },
    // 开闸
    startBar () {
      ipcRenderer.send('raise-bar')
    },
    parseCardCode (code) {
      const head = '注意！操作卡失败，请检查您的卡或联系服务台'

      if (code === -222) {
        return head + ' 密码装载错误！ '
      }
      if (code === -401) {
        return head + ' 卡操作频繁或认证有误, 无效卡！ '
      }

      if (code === -998) {
        return head + ' 写卡异常， 请检查卡类型! '
      }

      if (code === 2) {
        return head + ' 请重新放卡! '
      }

      if (code === 3) {
        return head + ' 防冲突出错! '
      }

      return head
    },
    // 读卡成功的回调信息
    handleSuccessMqttMessage (messageObject) {
      log('cms-gate-out 开始处理业务', messageObject, ', current IC', this.ic)
      if (messageObject.ic === this.ic) {
        // 开闸
        if (messageObject.dt.raiseBarStatus === 0) {
          // 为1时：再传一次后端获取写卡数据，做写卡操作；
          // 为0时：红色X显示，后端返回的信息信息
          // 为2时，黄色！显示，后端返回的提示信息
          if (messageObject.dt.messageLevel === 0) {
            this.showMsg(messageObject.dt.message, 'error')

            // 清除表单
            this.$refs.runBox.handleSuccess()
            return
          } else if (messageObject.dt.messageLevel === 2) {
            this.showMsg(messageObject.dt.message, '')
            // 清除表单
            this.$refs.runBox.handleSuccess()
            return
          } else {
            this.showMsg('未知的messageLevel' + messageObject.dt.messageLevel, '')
            // 清除表单
            this.$refs.runBox.handleSuccess()
            return
          }
        }
        this.startBar()

        // 开闸的情况， 提示错误信息
        if (messageObject.dt.messageLevel === 0) {
          setTimeout(() => {
            this.showMsg(messageObject.dt.message, 'error')

          }, 2000);

        } else if (messageObject.dt.messageLevel === 2) {
          setTimeout(() => {
            this.showMsg(messageObject.dt.message, '')
          }, 2000);
        }

        // 然后再写数据
        messageObject.dt.cpData.handlerType = 'createInfo'
        let qd = {
          dt: messageObject.dt.cpData,
          ic: GSAConstants().TOPIC_PREADVICE_GATE_OUT.ic,
          en: false
        }

        // 提交数据
        log('调用createInfo接口. --->')
        // this.showMsg('正在写卡', '')
        this.$refs.runBox.customSentMqttMsg(GSAConstants().TOPIC_PREADVICE_GATE_OUT.url, qd)
      } else if (messageObject.ic === GSAConstants().TOPIC_PREADVICE_GATE_OUT.ic) {

        // 为1时：再传一次后端获取写卡数据，做写卡操作；
        // 为0时：红色X显示，后端返回的信息信息
        // 为2时，黄色！显示，后端返回的提示信息
        if (messageObject.dt.messageLevel === 0) {
          this.showMsg(messageObject.dt.message, 'error')

          // 清除表单
          this.$refs.runBox.handleSuccess()
          return
        } else if (messageObject.dt.messageLevel === 1) {
          // // 提交数据， 调用写卡接口
          // this.$refs.runBox.doPageFun('submitCustomerUrl', GSAConstants().TOPIC_PREADVICE_MANUAL_OUT.url, GSAConstants().TOPIC_PREADVICE_MANUAL_OUT.ic)

          // 然后再写数据
          let qd = {
            dt: messageObject.dt.cpData,
            ic: GSAConstants().TOPIC_PREADVICE_MANUAL_OUT.ic,
            en: false
          }

          // 提交数据
          log('调用写卡接口. --->')
          this.$refs.runBox.customSentMqttMsg(GSAConstants().TOPIC_PREADVICE_MANUAL_OUT.url, qd)
        } else if (messageObject.dt.messageLevel === 2) {
          this.showMsg(messageObject.dt.message, '')
          // 清除表单
          this.$refs.runBox.handleSuccess()
          return
        }
      } else if (messageObject.ic === GSAConstants().TOPIC_PREADVICE_MANUAL_OUT.ic) {
        // 清除表单
        const qd = {
          p8: messageObject.dt.contInfo8,
          p9: messageObject.dt.contInfo9
        }
        ipcRenderer.send('write-card', qd)

        if (messageObject.dt.messageCode === 0) {
          this.showMsg(messageObject.dt.message, 'error')
        } else {
          this.showMsg('刷卡操作成功', 'green')
        }

        this.$refs.runBox.handleSuccess()
      }
    }
  },
}
</script>

<style scoped>
.app-box {
  padding: 0px;
}
</style>
